package org.wzl.todolisthelper.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.wzl.todolisthelper.convert.PeriodicTaskConvert;
import org.wzl.todolisthelper.domain.PeriodicTask;
import org.wzl.todolisthelper.dto.AddPeriodicTaskDTO;
import org.wzl.todolisthelper.service.PeriodicTaskService;
import org.wzl.todolisthelper.mapper.PeriodicTaskMapper;
import org.springframework.stereotype.Service;
import org.wzl.todolisthelper.utils.UserUtil;
import org.wzl.todolisthelper.vo.IndexTaskCardVO;
import org.wzl.todolisthelper.vo.PeriodicTaskDetailVO;

import java.util.List;

/**
* @author weizhilong
* @description 针对表【t_periodic_task】的数据库操作Service实现
* @createDate 2025-02-23 20:57:18
*/
@Service
public class PeriodicTaskServiceImpl extends ServiceImpl<PeriodicTaskMapper, PeriodicTask>
    implements PeriodicTaskService {

    @Resource
    private PeriodicTaskConvert periodicTaskConvert;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTask(AddPeriodicTaskDTO addPeriodicTaskDTO) {
        PeriodicTask periodicTask = periodicTaskConvert.toDO(addPeriodicTaskDTO);

        validTask(addPeriodicTaskDTO);
        save(periodicTask);

        //添加记录
    }

    @Override
    public List<IndexTaskCardVO> getAllCard() {
        List<PeriodicTask> allDO = list(Wrappers.<PeriodicTask>lambdaQuery()
                .eq(PeriodicTask::getCreateUserId, UserUtil.getCurrentUserId())
                .orderBy(true, false, PeriodicTask::getCreateDate));
        return allDO.stream().map(periodicTaskConvert::toVO).toList();
    }

    @Override
    public PeriodicTaskDetailVO getDetailById(String id) {
        return periodicTaskConvert.toDetailVO(getById(id));
    }

    //TODO 校验参数
    @SuppressWarnings("unchecked")
    private void validTask(AddPeriodicTaskDTO periodicTask) {
        String repetitionCycle = periodicTask.getRepetitionCycle();
//        Map<String, Object> filedMap = (LinkedHashMap<String, Object>) periodicTask.getPeriodicTaskDetail();
        boolean isValid = Boolean.TRUE;
        switch (repetitionCycle) {
            case "day":
//                filedMap.get("")
                break;
            case "week":
                break;
            case "month":
                break;
            default:
                throw new RuntimeException("周期类型错误");
        }
    }

}




